package com.abe.mvp.view.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.abe.config.Constants;
import com.abe.config.SettingParam;
import com.abe.mvp.contact.WebServerContact;
import com.abe.mvp.contact.WebServerContact.ExercideUploadView;
import com.abe.mvp.model.bean.CardReportBean;
import com.abe.mvp.model.bean.exercise.ExerciseSTGroupBean;
import com.abe.mvp.model.bean.http.ExerciseSTBean;
import com.abe.mvp.model.bean.http.ExerciseSTDataBean;
import com.abe.mvp.presenter.HttpExercisePresenter;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.mvp.view.adapter.ExerciseReportAdapter;
import com.abe.strengthtrainer.R;
import com.abe.utils.NormalUtils;
import com.abe.utils.ToastUtils;
import com.squareup.picasso.Picasso;

@SuppressLint({ "InflateParams", "SimpleDateFormat" })
@ContentView(R.layout.activity_exercise_report)
public class ExerciseDataActivity extends BaseActivity implements
		ExercideUploadView {

	@ViewInject(R.id.show_exercise_button_back)
	private LinearLayout show_exercise_button_back;
	@ViewInject(R.id.show_exercise_logo)
	private ImageView show_exercise_logo;

	@ViewInject(R.id.tabmenu_layout)
	private ImageView tabmenu_layout;

	@ViewInject(R.id.exercise_report_grid)
	private GridView exercise_report_grid;

	private WebServerContact.ExercisePresenter presenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		presenter = new HttpExercisePresenter(this);
		initView();
	}

	private void initView() {
		initBackground();
		show_exercise_button_back.setVisibility(View.VISIBLE);
		show_exercise_logo.setVisibility(View.VISIBLE);
		/** 锻炼报告 */
		Bundle bundle = getIntent().getExtras();
		ExerciseSTGroupBean groupBean = null;
		if (bundle != null) {
			groupBean = (ExerciseSTGroupBean) bundle.get("exercise");
		}

		if (groupBean == null) {
			groupBean = new ExerciseSTGroupBean();
			groupBean.setCalorie(0);
			groupBean.setCount(0);
			groupBean.setWeight(0);
			groupBean.setKeepTime(0);
		}
		/** 数据上传 */
		if (current_user.getFlag() != SettingParam.flag_user_tourist) {
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			List<ExerciseSTDataBean> stDataBeans = new ArrayList<ExerciseSTDataBean>();
			ExerciseSTDataBean stDataBean = new ExerciseSTDataBean();
			stDataBean.setCalorie((int) groupBean.getCalorie());
			stDataBean.setStrength(groupBean.getWeight());
			stDataBean.setTimes(groupBean.getCount());
			stDataBean.setTime(format.format(new Date()));
			stDataBean.setDuration(String.valueOf(groupBean.getKeepTime()));
			stDataBeans.add(stDataBean);
			ExerciseSTBean stBean = new ExerciseSTBean();
			stBean.setOpenid(current_user.getUser().getOpenid());
			stBean.setTrainningData(stDataBeans);
			presenter.uploadExerciseData(stBean);
		}
		/** 数据展示 */
		List<CardReportBean> cardReportBeans = new ArrayList<CardReportBean>();
		int drawable[] = { R.drawable.icon_dlcs, R.drawable.icon_dlsj,
				R.drawable.icon_dlkll };
		int[] names = { R.string.common_report_num,
				R.string.common_report_weight, R.string.common_report_cll };
		int[] units = { R.string.common_report_num_unit,
				R.string.common_report_weight_unit,
				R.string.common_report_cll_unit };
		for (int i = 0; i < 3; i++) {
			CardReportBean bean = new CardReportBean();
			bean.setName(NormalUtils.getString(names[i]));
			bean.setUnit(NormalUtils.getString(units[i]));
			bean.setDrawable(drawable[i]);
			bean.setType(Integer.class);
			if (i == 0) {
				bean.setNum(String.valueOf(groupBean.getCount()));
			} else if (i == 1) {
				bean.setNum(String.valueOf(groupBean.getWeight()));
			} else {
				bean.setNum(String.valueOf(Constants.keepDecimal(
						groupBean.getCalorie(), 1)));
			}
			cardReportBeans.add(bean);
		}
		ExerciseReportAdapter adapter = new ExerciseReportAdapter(this,
				cardReportBeans, scale);
		exercise_report_grid.setAdapter(adapter);
	}

	private void initBackground() {
		/** 菜单页背景 */
		Picasso.with(this).load(R.drawable.run_image_bg_05).centerCrop()
				.placeholder(R.drawable.app_theme_bg).resize(1152, 720)
				.into(tabmenu_layout);
	}

	@Event(value = R.id.show_exercise_button_back, type = View.OnClickListener.class)
	private void buttonBack(View view) {
		back();
	}

	@Override
	public void onRequestFail(String errorMsg) {
		ToastUtils.showShort(getApplicationContext(), errorMsg);
	}

	@Override
	public void onFinish(int type) {

	}

	@Override
	public void onExerciseUploadSuccess() {
		ToastUtils.showShort(getApplicationContext(), "锻炼数据已上传");
	}
}
